using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities;
using Sirenix.Utilities.Editor;
using UnityEditor;
using UnityEngine;
using YooAsset.Editor;
using YooAsset.Editor.AssetBundleOdinEditor;

public class AssetBundleGrouperOdinEditor : OdinMenuEditorWindow
{
    [MenuItem("YooAsset/OdinAssetBundleGrouper")]
    private static void Open()
    {
        var window = GetWindow<AssetBundleGrouperOdinEditor>();
        window.position = GUIHelper.GetEditorWindowRect().AlignCenter(1440, 500);
    }


    protected override OdinMenuTree BuildMenuTree()
    {
        var tree = new OdinMenuTree(true);
        tree.DefaultMenuStyle.IconSize = 28.00f;
        tree.Config.DrawSearchToolbar = true;

        AssetBundleGrouperOverview.Instance.UpdateCharacterOverview();
        foreach (var assetBundleGrouper in AssetBundleGrouperSettingData.Setting.Groupers)
        {
            tree.Add(assetBundleGrouper.GrouperName, assetBundleGrouper);
        }
        return tree;
    }

    protected override void OnBeginDrawEditors()
    {
        var selected = this.MenuTree.Selection.FirstOrDefault();
        var toolbarHeight = this.MenuTree.Config.SearchToolbarHeight;

        // Draws a toolbar with the name of the currently selected menu item.
        SirenixEditorGUI.BeginHorizontalToolbar(toolbarHeight);
        {
            if (selected != null)
            {
                GUILayout.Label(selected.Name);
            }

            if (SirenixEditorGUI.ToolbarButton(new GUIContent("添加新配置项")))
            {
                ScriptableObjectCreator.ShowDialog<AssetBundleGrouper>("Assets/YooAssetSetting/AssetBundleGroupers", obj =>
                {
                    obj.GrouperName = obj.name;
                    base.TrySelectMenuItemWithObject(obj); // Selects the newly created item in the editor
                });
            }

            if (SirenixEditorGUI.ToolbarButton(new GUIContent("导出XML")))
            {
                SelectFolder();
            }
            
            if (SirenixEditorGUI.ToolbarButton(new GUIContent("导入配置")))
            {
                if (EditorUtility.DisplayDialog("提示", "导入配置会覆盖当前修改内容，是否导入？", "确认", "取消"))
                {
                    string resultPath = EditorTools.OpenFilePath("Import XML", "Assets/", "xml");
                    if (string.IsNullOrEmpty(resultPath))
                    {
                        EditorUtility.DisplayDialog("提示", "未选择配置", "确认");
                        return;
                    }
                    AssetBundleGrouperConfig.ImportXmlConfig(resultPath);
                }
            }
        }
        SirenixEditorGUI.EndHorizontalToolbar();
    }

    private string SelectFolder()
    {
        string dataPath = Application.dataPath;
        string selectedPath = EditorUtility.OpenFolderPanel("Path", dataPath, "");
        if (!string.IsNullOrEmpty(selectedPath))
        {
            if (selectedPath.StartsWith(dataPath))
            {
                AssetBundleGrouperConfig.ExportXmlConfig(Application.dataPath + "/" + selectedPath.Substring(dataPath.Length + 1) + "/AssetBundleGrouperConfig.xml");
            }
            else
            {
                Debug.LogError(new GUIContent("Can not be outside of 'Assets/'!"));
            }
        }

        return "Assets/";
    }
}